<?php

class Application_Model_News extends Zend_Db_Table_Abstract
{
    protected $_name = 'news';
    //получаем все новости из базы данных
    public function getAllNews ($page = 0, $items_on_page = 10){
               
       $select = $this->select()->from($this->_name)->order(array("date DESC"));
        return $this->fetchAll($select)->toArray(); 

    }
    
    /*
      public function getByTypeID($type_id, $page = 0, $items_on_page = 10)
    {
        $select = $this->getAdapter()->select()
                                     ->from(array('o' => $this->_name),
                                             array('name', 'address', 'website', 'telephone',
                                                   'work_time', 'latitude', 'longitude', 'id'
                                                 )
                                         )
                                     ->join(array('ot' => 'organizations_type'),
                                                   'o.id = ot.organization_id'
                                         )
                                     ->where('ot.type_id = ?', $type_id);
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage($items_on_page);
        
        return $paginator;
    }
     */
    //удаляем выбранную новость
    public function deleteNews($id){ 
        $this->delete('id = ' .intval($id));
    }
    //добавление новости
    public function createNews($data){
        $this->insert($data);
    }
    
    public function getNews($id){
        $select = $this->select()->from($this->_name)
                       ->where($this->_name.'.id =?', intval($id));
        return $this->fetchAll($select)->toArray();
    }
    //обновляем новость
    public function updateNews($data, $id){
        $this->update($data, 'id =' . intval($id));
    }
    public function getTwoLastNews(){
              
        $this->db = Zend_Db_Table::getDefaultAdapter();
        $select = $this->db->query("SELECT name, text, date 
                                  FROM news 
                                  ORDER BY id DESC
                                  LIMIT 2");
        return $select; 
    }

}

